package org.ehe.erp.controller.product;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.monitor.MonitorMethod;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.product.bo.ErpProductArrivalBo;
import org.ehe.erp.domain.product.vo.ErpProductArrivalVo;
import org.ehe.erp.service.product.ErpProductArrivalService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ERP订单到货批次信息管理
 * @author :zhangnn
 * @className :ErpProductArrivalController
 * @description: TODO
 * @date 2025-08-19 09:18:30
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/product/arrival")
public class ErpProductArrivalController extends BaseController {

    private final ErpProductArrivalService arrivalService;

    /**
     * 查询到货批次信息列表
     */
    @SaCheckPermission("erp:arrival:list")
    @GetMapping("/list")
    public TableDataInfo<ErpProductArrivalVo> list(ErpProductArrivalBo bo, PageQuery pageQuery) {
        return arrivalService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取到货批次信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:arrival:view")
    @GetMapping("/{id}")
    public R<ErpProductArrivalVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(arrivalService.queryById(id));
    }

    /**
     * 新增到货批次信息
     */
    @SaCheckPermission("erp:arrival:add")
    @Log(title = "到货批次新增", linkType = "product", businessType = BusinessType.INSERT,  nameField = "no")
    @PostMapping("/create")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<ErpProductArrivalBo> bos) {
        // 校验付款单号是否存在
        //boolean exists = arrivalService.checkNoExists(bos.get(0).getNo());
//        if (exists) {
//            return R.fail("付款单号已存在");
//        }
        return toAjax(arrivalService.insertByBo(bos));
    }

    /**
     * 修改到货批次信息
     */
    @SaCheckPermission("erp:arrival:edit")
    @Log(title = "到货批次信息修改", linkType = "product", businessType = BusinessType.UPDATE,  nameField = "no")
    @PostMapping("/update")
    public R<Void> update(@Validated(EditGroup.class) @RequestBody List<ErpProductArrivalBo> bos) {
        // 校验付款单号是否存在（排除自身）
//        boolean exists = arrivalService.checkNoExistsExcludeSelf(bos.get(0).getNo(), bos.get(0).getId());
//        if (exists) {
//            return R.fail("付款单号已存在，修改失败！");
//        }
        return toAjax(arrivalService.updateByBo(bos));
    }

    /**
     * 删除到货批次信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:arrival:remove")
    @Log(title = "到货批次信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(arrivalService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据批次ID查询到货信息列表
     *
     * @param batchId 批次ID
     */
    @SaCheckPermission("erp:arrival:view")
    @GetMapping("/batch/{batchId}")
    public R<List<ErpProductArrivalVo>> getByBatchId(@NotNull(message = "批次ID不能为空")
                                                     @PathVariable Long batchId) {
        return R.ok(arrivalService.queryByBatchId(batchId));
    }

    /**
     * 根据供应商ID查询到货信息列表
     *
     * @param supplierId 供应商ID
     */
    @GetMapping("/supplier/{supplierId}")
    public R<List<ErpProductArrivalVo>> getBySupplierId(@NotNull(message = "供应商ID不能为空")
                                                        @PathVariable Long supplierId) {
        return R.ok(arrivalService.queryBySupplierId(supplierId));
    }

    /**
     * 批量更新到货状态
     *
     * @param ids 主键数组
     * @param status 状态
     */
    @SaCheckPermission("erp:arrival:updateStatue")
    @Log(title = "批量更新到货状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public R<Void> updateStatus(@RequestParam Long[] ids, @RequestParam Integer status) {
        return toAjax(arrivalService.updateStatusByIds(List.of(ids), status));
    }

}
